package com.dhu.controller;

import com.alibaba.fastjson.JSONObject;
import com.dhu.dao.ReplyInnerMapper;
import com.dhu.dao.UserMapper;
import com.dhu.pojo.Reply;
import com.dhu.pojo.ReplyInner;
import com.dhu.pojo.User;
import com.dhu.service.ReplyService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class ReplyController {
    @Autowired
    private ReplyService replyService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ReplyInnerMapper replyInnerMapper;

    // 获取帖子的回复
    @GetMapping("/getPostReply")
    private String getPostReply(int id) {
        JSONObject res = new JSONObject();
        List<Reply> replies = replyService.selectByPostId(id);
        if (replies != null) {
            res.put("code", 200);
            res.put("message", "查询帖子回复成功");
            res.put("replyList", replies);
        } else {
            res.put("code", 403);
            res.put("message", "查询帖子回复失败");
        }
        return res.toJSONString();
    }

    // 增加回复
    @RequestMapping("/addReply")
    private String addReply(@RequestBody Reply reply) {
        JSONObject res = new JSONObject();
        if (replyService.addReply(reply) == 1) {
            res.put("code", 200);
            res.put("message", "回复成功");
        } else {
            res.put("code", 403);
            res.put("message", "回复失败");
        }
        return res.toJSONString();
    }

    // 增加回复的回复
    @RequestMapping("/addReplyInner")
    private String addReplyInner(@RequestBody ReplyInner replyInner) {
        JSONObject res = new JSONObject();
        if (replyInnerMapper.addReplyInner(replyInner) == 1) {
            res.put("code", 200);
            res.put("message", "回复成功");
        } else {
            res.put("code", 403);
            res.put("message", "回复失败");
        }
        return res.toJSONString();
    }
}
